// stdx/clonex.rs
// CRITICAL: Clone 与 Copy trait 的示例与慨念。
// AUTO_COMMENT: 展示何时实现 Clone/Copy，以及派生的用法。

#[allow(dead_code)]
pub fn stdx_examples_clonex() {
    #[derive(Clone, Copy, Debug)]
    struct Small(i32);

    #[derive(Clone, Debug)]
    struct Large {
        data: Vec<i32>,
    }

    let s = Small(1);
    let s2 = s; // Copy 类型可以按位复制
    let l = Large {
        data: vec![1, 2, 3],
    };
    let l2 = l.clone();

    let _ = (s2, l2);

    // CRITICAL: 手动实现 Clone
    #[derive(Debug)]
    struct ManualClone {
        value: i32,
    }

    impl Clone for ManualClone {
        fn clone(&self) -> Self {
            ManualClone { value: self.value }
        }
    }

    let mc = ManualClone { value: 42 };
    let mc_cloned = mc.clone();

    // CRITICAL: Copy trait 要求 Clone
    #[derive(Clone, Copy, Debug)]
    struct Point {
        x: f64,
        y: f64,
    }

    let p1 = Point { x: 1.0, y: 2.0 };
    let p2 = p1; // Copy
    let p3 = p1.clone(); // 也可以 clone

    // CRITICAL: 集合的克隆
    let vec_original: Vec<i32> = vec![1, 2, 3];
    let vec_cloned = vec_original.clone();

    let _ = (mc_cloned, p2, p3, vec_cloned);
}
